/*******************************************************************************
 * File name:     main.c
 * Description:   HTX project- it's a basic framework for LPC2478. It contains
 *                the following directories:
 *                1. _init- low level initialization of CPU, peripherals init
 *                as MAM, PLL, fast I/O and clock for whole CPU modules
 *                2. _drivers- API for LPC2478 peripherals
 *                3. _utils - utilities like printf, itoa, etc
 * Project:       HTX
 * Target:        LPC2478
 * Compiler:      arm-none-eabi-gcc
 * Date:          2010-12-02
 * Author:        Kuba
 * Based on: -
 ******************************************************************************/

/*==============================================================================
 Includes
==============================================================================*/
# include <stdbool.h>
# include "LPC24xx.h"
# include "typedef.h"
# include "init.h"
# include "irq.h"
# include "board.h"

# include "timer.h"
# include "uart.h"    /* it's an output for tests with Olimex board */
# include "printf.h"


/*==============================================================================
 Defines
==============================================================================*/

/*==============================================================================
 Globals
==============================================================================*/
struct uart u0;
static struct timerConf tmr;
static timer t;

/*==============================================================================
 Static function prototypes
==============================================================================*/
static void jtagDelay   (void);
static void swiLedOn    (void);
static void swiLedOff   (void);
static void timerService(void);

/*==============================================================================
 Main function
==============================================================================*/
int main(void)
{
   /* at the beginning of the main, the following functions are called:
    * - jtagDelay ();   delay necessary for JTAG proper work,
    * - systemInit ();  this function sets MAM (Memory Accelerator Module), PLL,
    *                   fast I/O, maximum speed for peripherals, interrupt vector
    */
   jtagDelay();
   systemInit ();
   vicInit();
   irqEnableCpsr();

   /* PERIPHERALS INITIALIZATION */
   /* uart */
   u0.uartNo            = uart2;
   u0.baud              = baud9600;
   u0.wordLen           = word8BIT;
   u0.stopBit           = stop1BIT;
   u0.parity            = parityDISABLE;
   u0.paritySel         = parityODD;
   u0.breakCtrl         = breakCTRL_DISABLE;
   u0.fifo              = 0;
   u0.intrpt            = 0;
   u0.intrptTxTrig      = 0;
   u0.intrptRxTrig      = 0;
   u0.buffers.txBuff    = 0;
   u0.buffers.txBuffSize= 0;
   u0.buffers.rxBuff    = 0;
   u0.buffers.rxBuffSize= 0;
   uartInit (&u0);   /* messages via RS232 */
   uprintf("%s\n", "_framework");

   /* leds */
   ledInit();
   /* timer */
   tmr.number = timer0;
   tmr.priority = irqLOWEST_PRIORITY;
   tmr.match[1].interrupt = matchINTERRUPT_ENABLE;
   tmr.match[1].reset = matchRESET_ENABLE;
   tmr.match[1].delay = 72000000;
   tmr.match[1].matchService = timerService; /* blink led */
   t = timerInit (&tmr);
   timerMatchEnable(t);

   uint32 i;

   while (1)
   {
      //LED0_ON = LED0;
      //swiLedOn();
      for (i = 0; i < 5000000; ++i);
      //LED0_OFF = LED0;
      //swiLedOff();
      for (i = 0; i < 5000000; ++i);
   }
   return 0;
}

/*==============================================================================
 Static function definitions
==============================================================================*/
/*------------------------------------------------------------------------------
 function name:      jtagDelay
 description:     function provides a delay required by jtag
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void jtagDelay (void)
{
   volatile unsigned int i;
   for (i = 0; i < 200000; ++i);
}

/*------------------------------------------------------------------------------
 function name:   swiLedOn
 description:     led effect by SWI interrupt
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void swiLedOn ()
{
   asm volatile ("swi #1\n\t");
}

/*------------------------------------------------------------------------------
 function name:   swiLedOff
 description:     led effect by SWI interrupt
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void swiLedOff ()
{
   asm volatile ("swi #2\n\t");
}

/*------------------------------------------------------------------------------
 function name:   timerService
 description:
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void timerService (void)
{
   ledBlink(0);          /* change LED pin's state */
}

/******************************************************************************
* END OF FILE
******************************************************************************/
